<template>
  <div class="set-wrapper">
    <el-form :inline="true">
      <el-form-item label="所属部门：">
        <el-select style="width: 150px;" clearable v-model="searchDep" placeholder="请选择">
          <el-option
            v-for="item in departments"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考核类型：">
        <el-select style="width: 150px;" clearable v-model="searchClass" placeholder="请选择">
          <el-option
            v-for="item in classType"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="searchLoading" @click="searchItem(true)">查询</el-button>
        <el-button type="primary" @click="openItemDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table :height="tableHeight" :data="tableData" border style="width: 100%">
        <el-table-column label="序号" type="index" width="100" align="center"></el-table-column>
        <el-table-column label="所属部门" prop="deptName" width="120" align="center"></el-table-column>
        <el-table-column label="考核类型" prop="parentName" min-width="100" align="center"></el-table-column>
        <el-table-column label="考核内容" prop="name" min-width="180" align="left" header-align="center"></el-table-column>
        <el-table-column label="考核关键点" min-width="180" align="left" header-align="center">
          <template slot-scope="scope">
            <div style="white-space: pre-line;">{{scope.row.keyPoint}}</div>
          </template>
        </el-table-column>
        <el-table-column label="考核参考指标" min-width="180" align="left" header-align="center">
          <template slot-scope="scope">
            <div style="white-space: pre-line;">{{scope.row.indexPoint}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center"  width="180">
            <template slot-scope="scope">
              <el-button @click="openItemDialog(scope.row)" type="text" size="small">编辑</el-button>
              <el-button @click="removeItem(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog title="考核条目" :visible.sync="itemDialog" width="600px" :close-on-click-modal="false">
      <el-form :model="form" style="padding-right:100px;">
        <el-form-item label="所属部门：" :label-width="formLabelWidth">
          <el-select v-model="form.deptId" :disabled="formStatus === 'edit'" ref="labelSelected" placeholder="所属部门">
            <el-option
              v-for="item in formDepartments"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核类型：" :label-width="formLabelWidth">
          <el-select v-model="form.pid" :disabled="formStatus === 'edit'" placeholder="考核类型">
            <el-option
              v-for="item in formClassType"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核分值倍数：" :label-width="formLabelWidth">
          <el-input-number v-model="form.scoresPower" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="考核内容：" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="5" placeholder="请输入考核内容" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="考核关键点：" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="5" placeholder="请输入考核关键点" v-model="form.keyPoint"></el-input>
        </el-form-item>
        <el-form-item label="考核参考指标：" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="5" placeholder="请输入考核参考指标" v-model="form.indexPoint"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="itemDialog = false">取 消</el-button>
        <el-button :loading="saveLoading" type="primary" @click="addItem">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'

  export default {
    data () {
      return {
        searchLoading: false,
        saveLoading: false,
        searchDep:'',
        pageSize: 10,
        page:1,
        totalPage: 0,
        searchClass: '',
        departments: [],
        formStatus: 'add',
        classType: [],
        formDepartments: [],
        formClassType: [],
        itemDialog: false,
        form: {
          deptId: 0,
          deptName: '',
          dingId: '',
          pid: '',
          scoresPower: 1,
          indexPoint: '',
          keyPoint:'' ,
          name: ''
        },
        formLabelWidth: '120px',
        tableData: [],
        tableHeight: window.tableHeight
      }
    },
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 270
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 270
      }
    },
    mixins: [dingMixin],
    mounted(){
      //获取部门列表
      this.$http({
        url: this.$http.adornUrl('/sys/dept/list'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.departments = data.data;
          this.formDepartments = data.data
        }
      })

      //获取考核大项列表
      this.$http({
        url: this.$http.adornUrl('/kpi/item/listBigItem'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.classType = data.data;
          this.formClassType = data.data
        }
      })

      this.searchItem(true);
    },
    methods: {
      //获取小项列表
      searchItem(init) {
        if(init) this.page = 1;
        this.searchLoading = true;
        this.$http({
          url: this.$http.adornUrl('/kpi/item/listItem'),
          method: 'post',
          data: this.$http.adornParams({
            'deptId': this.searchDep,
            'pid': this.searchClass,
            'page': this.page,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableData = data.data.list;
            this.totalPage = data.data.totalCount
          }
        }).finally(() => {
          this.searchLoading = false;
        })
      },

      //添加小项
      addItem() {
        this.form.deptName = this.$refs['labelSelected'].selectedLabel;
        this.form.dingId = this.userInfo.dingId;

        if(!this.form.deptName) return this.$message.error('请选择部门');
        if(!this.form.pid) return this.$message.error('请选择考核类型');
        if(!this.form.name) return this.$message.error('请输入考核内容');
        if(!this.form.keyPoint) return this.$message.error('请输入考核关键点');
        if(!this.form.indexPoint) return this.$message.error('请考核参考指标');

        this.saveLoading = true;
        this.$http({
          url: this.formStatus === 'add' ? this.$http.adornUrl('/kpi/item/save') : this.$http.adornUrl('/kpi/item/update'),
          method: 'post',
          data: this.$http.adornParams(this.form)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.itemDialog = false;
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.searchItem();
          }else {
            this.$message.error(data.msg)
          }
        }).finally(() => {
          this.saveLoading = false;
        })
      },

      //打开条目窗口
      openItemDialog(item) {
        this.itemDialog = true;
        if(item) {
          this.itemDialog = true;
          this.form = Object.assign({}, item);
          this.formStatus = 'edit'
        }else {
          this.form = {
            deptId: '',
            deptName: '',
            dingId: '',
            pid: '',
            scoresPower: 1,
            indexPoint: '',
            keyPoint:'' ,
            name: ''
          }
          if(this.searchDep) this.form.deptId = this.searchDep;
          if(this.searchClass) this.form.pid = this.searchClass;
          this.formStatus = 'add'
        }
      },
      sizeChangeHandle(limit) {
        this.pageSize = limit;
        this.searchItem(true);
      },
      currentChangeHandle(page) {
        this.page = page;
        this.searchItem();
      },

      //删除条目
      removeItem(item) {
        this.$confirm('此操作将永久删除该条目, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
           this.$http({
            url: this.$http.adornUrl('/kpi/item/delete'),
            method: 'post',
            data: [item.id]
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success'
              });
              this.searchItem();
            }else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
